package com.joinlabs.action;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.joinlabs.R;
import com.joinlabs.entity.User;
import com.joinlabs.service.IUserServiceZ;
import com.joinlabs.service.SmsService;

@Controller
@RequestMapping("/regiest")
public class RegAction {
	private static String reyzm="00";

	@Resource
	private SmsService smsService;

	@Resource
	private SmsService sms;
	
	@Resource	
	private IUserServiceZ userserviceZ;
	
	//访问注册界面S
	@RequestMapping("/reg")
	public ModelAndView reg(){
		return new ModelAndView("/regiest/regiest");
	}
	//检查手机号是否被注册
	@RequestMapping("/check")
	public void checkReg(HttpServletRequest request,HttpServletResponse response) throws IOException {
		String tel = request.getParameter("tel");
				
		if(userserviceZ.findUserAccount(tel)==null) {
			response.getWriter().write("suc");
		} else {
			response.getWriter().write("erroy");
		}
		
	}

	
	
	
	//发送短信验证
		@RequestMapping("/send")
		public void sends(HttpServletRequest request,HttpServletResponse response) {
			String tel = request.getParameter("tel");
			reyzm = sms.send(tel);
			
//			System.out.println("甲方开具安居房就进度款加法看大家安静阿加解决就就kjj阶段空间加减速就就就就");
//			System.out.println(reyzm);
			
		}
		
		
		//检查验证码是否正确
		@RequestMapping("/checkyzm")
		public void checktel(HttpServletRequest request,HttpServletResponse response,ModelMap map) throws IOException {
			String yzm1 = request.getParameter("telyzm");
			System.out.println(yzm1+reyzm);
			if(reyzm.equals(yzm1)) {
				response.getWriter().write("suc");
			} else {
				response.getWriter().write("erroy");
			}
			
			
		}
		
		
		//将用户添加到数据库
	@RequestMapping("/insert")
	public ModelAndView insert(HttpServletRequest request,HttpServletResponse response) {
//		System.out.println("甲方开具安居房就进度款加法看大家安静阿加解决就就kjj阶段空间加减速就就就就");
		String username = request.getParameter("userName");
		String useraccount = request.getParameter("userAccount");
		String userpwd = request.getParameter("userPassword");
		Short u = 0;
		User user = new User();
		user.setUserAccount(useraccount);
		user.setUserName(username);
		user.setUserPassword(userpwd);
		user.setUserBlacklist(u);
		user.setUserFans((long)0);
		user.setUserWbnum((long)0);
		user.setUserConcernnum((long)0);
		user.setUserHeadpath("/img/user/1.jpg");
		userserviceZ.insert(user);
		System.out.println(user);
		return new ModelAndView("redirect:/login/index");
		
	}
		
	
}
